package com.ellis.ellisSpringboot02.service.impl;

import com.ellis.ellisSpringboot02.dao.UserMapper;
import com.ellis.ellisSpringboot02.entity.dto.UserDto;
import com.ellis.ellisSpringboot02.entity.po.UserPo;
import com.ellis.ellisSpringboot02.entity.vo.ResultVo;
import com.ellis.ellisSpringboot02.entity.vo.UserVo;
import com.ellis.ellisSpringboot02.service.UserService;
import com.ellis.ellisSpringboot02.utils.ResultUtils;
import com.ellis.ellisSpringboot02.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.logging.Logger;

/**
 * 用户服务层实现类
 */
@Service
public class UserServiceImpl implements UserService {

    Logger logger = Logger.getLogger("UserServiceImpl");

    //引入用户dao层
    @Resource
    UserMapper userMapper;


    @Override
    public String getUserNums() {
        return "用户的总数量为："+userMapper.getUserNums();
    }

    @Override
    public UserVo getUserDetails(Integer id) {
        if (id == null){
            //再项目中，尽量使用logger来代替System.out.println();
            logger.warning("传入的用户id不能为空");
            UserVo userVo = new UserVo();
            userVo.setMessage("传入的用户id不能为空");
            return userVo;
        }
        UserPo userPo = userMapper.getUserDetails(id);
        //如果UserPo为空或者UserPo的id为空，都直接返回空即可
        if (userPo == null || userPo.getId()==null) {
            return new UserVo();
        }
        UserVo userVo = new UserVo();

        //UserPo中的属性复制给UserVo
        BeanUtils.copyProperties(userPo,userVo);

        return userVo;
    }

    @Override
    public ResultVo setUserInfo(UserDto userDto) {
        if (userDto == null ) {
            return ResultUtils.failed("当前对象不能为空");
        }
        if (StringUtils.isBlank(userDto.getName())) {
            return ResultUtils.failed("用户名称不能为空或者空串");
        }
        UserPo userPo = new UserPo();
        BeanUtils.copyProperties(userDto,userPo);
        Integer integer = userMapper.setUserInfo(userPo);
        if (integer > 0) {
            return ResultUtils.success("添加成功");
        }
        return ResultUtils.failed("添加失败");
    }

    @Override
    public ResultVo updateUserInfo(UserDto userDto) {
        if (userDto == null || userDto.getId() == null) {
            return ResultUtils.failed("当前对象或者当前对象id不能为空");
        }
        if (StringUtils.isBlank(userDto.getName())) {
            return ResultUtils.failed("用户名称不能为空或者空串");
        }
        UserPo userPo = new UserPo();
        BeanUtils.copyProperties(userDto,userPo);
        Integer integer = userMapper.updateUserInfo(userPo);
        if (integer > 0) {
            return ResultUtils.success("更新成功");
        }
        return ResultUtils.failed("更新失败");
    }

    @Override
    public ResultVo deleteUserInfo(Integer id) {
        if (id == null){
            //再项目中，尽量使用logger来代替System.out.println();
            logger.warning("传入的用户id不能为空");
            return ResultUtils.failed("传入的用户id不能为空");
        }
        Integer integer = userMapper.deleteUserInfo(id);
        if (integer > 0) {
            return ResultUtils.success("删除成功");
        }
        return ResultUtils.failed("删除失败");
    }
}
